﻿package frEngine.core.mesh
{
    import baseEngine.basic.*;
    import baseEngine.core.*;
    import baseEngine.system.*;
    import flash.events.*;
    import flash.geom.*;
    import frEngine.*;
    import frEngine.event.*;
    import frEngine.loaders.resource.info.*;
    import frEngine.manager.*;
    import frEngine.render.layer.*;
    import frEngine.shader.filters.fragmentFilters.*;
    import frEngine.shader.filters.vertexFilters.*;

    public class NormalMesh3D extends Mesh3D
    {
        protected var _meshUrl:String;
        private var _useAlpha:Boolean;
        private var _meshInfo:MeshInfo;

        public function NormalMesh3D(param1:String)
        {
            super(param1, false, null);
            this.layer = Layer3DManager.modelLayer0;
            return;
        }// end function

        public function reinit(param1:String, param2:RenderList) : void
        {
            this.renderList = param2;
            if (param1)
            {
                this.meshUrl = param1;
            }
            return;
        }// end function

        public function get meshUrl() : String
        {
            return this._meshUrl;
        }// end function

        public function set meshUrl(param1:String) : void
        {
            if (this._meshUrl && this._meshUrl != param1)
            {
                Resource3dManager.instance.unLoad(this._meshUrl, this.hasLoadedBytes);
            }
            this._meshUrl = param1;
            this._meshInfo = null;
            if (param1)
            {
                Resource3dManager.instance.load(param1, this.hasLoadedBytes, loadPriority);
            }
            return;
        }// end function

        override public function setShaderBase(param1:VertexFilter, param2:FragmentFilter) : void
        {
            super.setShaderBase(param1, param2);
            this.setUseAlpha();
            return;
        }// end function

        private function setUseAlpha() : void
        {
            if (this.material.materialParams.vertexFilter && this.material.materialParams.vertexFilter.useAlpha != this._useAlpha)
            {
                this.material.materialParams.vertexFilter.useAlpha = this._useAlpha;
                this.material.materialParams.needRebuild = true;
            }
            return;
        }// end function

        private function hasLoadedBytes(param1:MeshInfo) : void
        {
            this._meshInfo = param1;
            if (param1.proceedParsed)
            {
                this.checkMeshComplete();
            }
            else
            {
                FrEventDispatcher.instance.proxyAddEventListener(param1, Engine3dEventName.PARSE_RESOURCE_FINISH, this.checkMeshComplete);
            }
            return;
        }// end function

        private function checkMeshComplete(event:Event = null) : void
        {
            if (!this._meshInfo)
            {
                return;
            }
            FrEventDispatcher.instance.proxyRemoveEventListener(this._meshInfo, Engine3dEventName.PARSE_RESOURCE_FINISH, this.checkMeshComplete);
            this.addSurface(this._meshInfo.surface3d);
            this._useAlpha = this._meshInfo.useAlpha;
            if (!this.material)
            {
                this.setMaterial(Global3D.nullBitmapDataTexture, Texture3D.MIP_NONE, "nullBitmapData");
            }
            else
            {
                this.setUseAlpha();
            }
            FrEventDispatcher.instance.proxyDispatchEvent(this, Engine3dEventName.PARSE_MESH_FINISH);
            return;
        }// end function

        override public function dispose(param1:Boolean = true) : void
        {
            Resource3dManager.instance.unLoad(this._meshUrl, this.hasLoadedBytes);
            super.dispose(param1);
            this._meshUrl = null;
            this._meshInfo = null;
            return;
        }// end function

        private function getMatrix(param1:Array) : Matrix3D
        {
            var _loc_2:* = new Vector3D(param1[0], param1[2], param1[1], 0);
            var _loc_3:* = new Vector3D(param1[3], param1[5], param1[4], 0);
            var _loc_4:* = new Vector3D(param1[6], param1[8], param1[7], 0);
            var _loc_5:* = new Vector3D(param1[9], param1[11], param1[10], 1);
            var _loc_6:* = new Matrix3D();
            _loc_6.copyColumnFrom(0, _loc_2);
            _loc_6.copyColumnFrom(2, _loc_3);
            _loc_6.copyColumnFrom(1, _loc_4);
            _loc_6.copyColumnFrom(3, _loc_5);
            return _loc_6;
        }// end function

    }
}
